﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Shell.Enums;
using Company.Shell.Interfaces;
using LiveCharts.Wpf;
using System.Windows;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    public class ProgramViewModel: ObservableObject
    {
        private CartesianChart ? chart;// 图表控件引用
        public ICommand ? MouseWorkCommand { get;  }
        public ICommand ChartClickCommand { get; }

        public ICommand? ChartLoadedCommand { get; }
        public ICommand? DecreaseXAxisCommand { get; }
        public ICommand? IncreaseXAxisCommand { get; }


        public IMouseManager MouseManager { get;  }
        public IProgramManager ProgramManager { get;  }
        public ProgramViewModel(IProgramManager programManager, 
                                IMouseManager mouseManager)
        {
            ProgramManager = programManager;
            MouseManager = mouseManager;
            MouseWorkCommand = new RelayCommand<MouseWorkMode>(OnMouseWorkCommand);
            ChartClickCommand = new RelayCommand<MouseButtonEventArgs>(OnChartClickCommand);
            ChartLoadedCommand = new RelayCommand<RoutedEventArgs>(OnChartLoadedCommand);
            IncreaseXAxisCommand = new RelayCommand(() => ProgramManager.Program.IncreaseXAxis());
            DecreaseXAxisCommand = new RelayCommand(() => ProgramManager.Program.DecreaseXAxis());
        }

        private void OnChartLoadedCommand(RoutedEventArgs? args)
        {
            if (chart == null)
            {
                chart = args?.OriginalSource as CartesianChart;// 获取图表控件

            }
        }

        private void OnChartClickCommand(MouseButtonEventArgs? args)
        {

            if (chart == null || args == null) return;
            var position = args.GetPosition(chart);// 获取鼠标点击位置
            double x = GetXFromPosition(position.X, chart);
            double y = GetYFromPosition(position.Y, chart);
            ProgramManager.Add(new LiveCharts.Defaults.ObservablePoint(x, y), MouseManager.WorkMode);
        }

        private double GetYFromPosition(double y, CartesianChart chart)
        {
            var yAxis = chart.AxisY.FirstOrDefault();// 获取Y轴
            if (yAxis == null) return 0;
            double chartHeight = chart.ActualHeight;
            double min = yAxis.MinValue;
            double max = yAxis.MaxValue;
            return max - (y / chartHeight) * (max - min);// 线性映射
        }

        private double GetXFromPosition(double x, CartesianChart chart)
        {
            var xAxis = chart.AxisX.FirstOrDefault();// 获取X轴
            if (xAxis == null) return 0;
            double chartWidth = chart.ActualWidth;
            double min = xAxis.MinValue;
            double max = xAxis.MaxValue;
            return min + (x / chartWidth) * (max - min);// 线性映射

        }


        private void OnMouseWorkCommand(MouseWorkMode mode)
        {
            (ProgramManager.ShowTemperature, ProgramManager.ShowHumidity) = mode switch
            {
                MouseWorkMode.默认操作 => (Visibility.Visible, Visibility.Visible),
                MouseWorkMode.编制温度 => (Visibility.Visible, Visibility.Collapsed),
                MouseWorkMode.编制湿度 => (Visibility.Collapsed, Visibility.Visible),
                _ => (ProgramManager.ShowTemperature, ProgramManager.ShowHumidity) // 保持当前值
            }; 
        }

        private void IncreaseXAxis()
        {
        }
    }
}
